<template>
  <div class="admin-home">
    <h1>校园维修 - 管理员端</h1>
    <div class="charts-container">
      <div class="repair-history">
        <h2>报修历史</h2>
        <div class="record-list-container">
          <ul class="record-list">
            <li v-for="(record, index) in visibleRecords" :key="index" class="record-item">
              <span>{{ record.date }} - {{ record.title }}</span>
            </li>
          </ul>
        </div>
      </div>
      <div class="repair-personnel">
        <h2>维修人员名单</h2>
        <div class="personnel-list-container">
          <ul class="personnel-list">
            <li v-for="(person, index) in visiblePersonnel" :key="index" class="personnel-item">
              <img :src="person.avatar" alt="头像" class="personnel-avatar" />
              <span class="personnel-name">{{ person.name }}</span>
              <span class="personnel-type">{{ person.type }}</span>
              <span class="personnel-phone">{{ person.phone }}</span>
            </li>
          </ul>
        </div>
      </div>
      <div id="completed-this-week" class="chart"></div>
      <div id="incomplete-tasks" class="chart"></div>
      <div id="completion-rate" class="chart"></div>
      <div id="other-chart" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { ref, onMounted, onBeforeUnmount } from 'vue';

const chartInstances = {};

const records = ref([
  { title: '教室灯管更换', date: '2024年07月11日' },
  { title: '宿舍水龙头修理', date: '2024年06月25日' },
  { title: '图书馆空调维护', date: '2024年05月15日' },
  { title: '操场跑道修复', date: '2024年04月20日' },
  { title: '教学楼电梯检修', date: '2024年03月30日' },
  { title: '实验室设备维修', date: '2024年03月10日' },
  { title: '宿舍楼屋顶修缮', date: '2024年02月05日' },
  { title: '体育馆灯光升级', date: '2024年01月22日' }
]);

const personnel = ref([
  { avatar: 'src/assets/工人头像.jpg', name: '张三', type: '电工', phone: '123-456-7890' },
  { avatar: 'src/assets/工人头像.jpg', name: '李四', type: '水工', phone: '123-456-7891' },
  { avatar: 'src/assets/工人头像.jpg', name: '王五', type: '维修工', phone: '123-456-7892' },
  { avatar: 'src/assets/工人头像.jpg', name: '赵六', type: '电工', phone: '123-456-7893' },
  { avatar: 'src/assets/工人头像.jpg', name: '钱七', type: '木工', phone: '123-456-7894' }
]);

const visibleRecords = ref(records.value.slice(0, 5));
let currentIndex = 0;

const updateVisibleRecords = () => {
  currentIndex = (currentIndex + 1) % records.value.length;
  visibleRecords.value = records.value.slice(currentIndex, currentIndex + 5);
};

const visiblePersonnel = ref(personnel.value.slice(0, 5));
let personnelIndex = 0;

const updateVisiblePersonnel = () => {
  personnelIndex = (personnelIndex + 1) % personnel.value.length;
  visiblePersonnel.value = personnel.value.slice(personnelIndex, personnelIndex + 5);
};

let intervalId;
let personnelIntervalId;

onMounted(() => {
  initializeCharts();
  intervalId = setInterval(updateVisibleRecords, 3000); // 每3秒更新一次
  personnelIntervalId = setInterval(updateVisiblePersonnel, 3000); // 每3秒更新一次
});

onBeforeUnmount(() => {
  destroyCharts();
  clearInterval(intervalId);
  clearInterval(personnelIntervalId);
});

const initializeCharts = () => {
  const chartIds = [
    'completed-this-week',
    'incomplete-tasks',
    'completion-rate',
    'other-chart'
  ];

  chartIds.forEach(id => {
    if (chartInstances[id]) {
      chartInstances[id].dispose();
    }
    chartInstances[id] = echarts.init(document.getElementById(id));
  });

  chartInstances['completed-this-week'].setOption({
    title: { text: '本周完成数' },
    tooltip: {},
    series: [
      {
        name: '完成数',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 10, name: '周一' },
          { value: 15, name: '周二' },
          { value: 25, name: '周三' },
          { value: 20, name: '周四' },
          { value: 35, name: '周五' },
        ],
      },
    ],
  });

  chartInstances['incomplete-tasks'].setOption({
    title: { text: '未完成数量' },
    tooltip: {},
    series: [
      {
        name: '未完成',
        type: 'gauge',
        detail: { formatter: '{value}%' },
        data: [{ value: 40, name: '未完成' }],
      },
    ],
  });

  chartInstances['completion-rate'].setOption({
    title: { text: '完成率' },
    tooltip: {},
    xAxis: { type: 'category', data: ['1月', '2月', '3月', '4月', '5月', '6月'] },
    yAxis: { type: 'value' },
    series: [
      {
        data: [50, 70, 60, 90, 80, 100],
        type: 'line',
        smooth: true,
      },
    ],
  });

  chartInstances['other-chart'].setOption({
    title: { text: '其他信息' },
    tooltip: {},
    xAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] },
    yAxis: { type: 'value' },
    series: [{ type: 'bar', data: [30, 40, 50, 60, 70] }],
  });
};

const destroyCharts = () => {
  Object.values(chartInstances).forEach(instance => {
    if (instance) {
      instance.dispose();
    }
  });
};
</script>

<style scoped>
.admin-home {
  text-align: center;
  margin-top: 20px;
  background-image: url('src/assets/bg-1.jpg');
  background-size: cover;
  background-attachment: fixed;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.admin-home h1 {
  font-size: 2em;
  color: #333;
}

.charts-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  padding: 20px;
}

.chart {
  width: 100%;
  height: 400px;
}

.repair-history, .repair-personnel {
  width: 100%;
  height: 400px;
  overflow: hidden;
}

.repair-history h2, .repair-personnel h2 {
  text-align: left;
  font-size: 1.5em;
}

.record-list-container, .personnel-list-container {
  height: calc(100% - 40px);
  overflow: hidden;
  position: relative;
}

.record-list, .personnel-list {
  list-style: none;
  padding: 0;
  margin: 0;
  position: absolute;
  width: 100%;
  animation: slide 10s infinite linear;
}

.record-item, .personnel-item {
  font-size: 1.2em;
  margin: 10px 0;
  display: flex;
  align-items: center;
}

.personnel-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 10px;
}

.personnel-name, .personnel-type, .personnel-phone {
  flex: 1;
  text-align: left;
}

@keyframes slide {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-100%);
  }
}
</style>
